/**
 *
 */
package idao.engine.executor;

import idao.DaoRuntimeException;
import idao.Update;

import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Executor for update sql statement.
 * 
 * @author huhao
 */
class UpdateExecutor extends SqlExecutor
{

    boolean noReturnValue = false;

    UpdateExecutor(Method m)
    {
        Class<?> rt = m.getReturnType();
        if (rt == Void.TYPE) {
            noReturnValue = true;
        } else if (rt != Integer.TYPE) {
            throw new DaoRuntimeException("Update method's return type must be 'int' or 'void'!");
        }
        this.parseSqlAndParameters(m.getAnnotation(Update.class).value());
    }

    @Override
    protected Object doExecute(PreparedStatement ps) throws SQLException
    {
        int updateCount = ps.executeUpdate();
        return noReturnValue ? (Void)null : updateCount;
    }

}
